<?php
include_once('Filters.php'); 
$CarsInfo=new Filter();
$Cars=$CarsInfo->getCars();
$search=array();
$startYear=0;
$endYear=0;

$found=0;
if(isset($_GET['Condition']))
{
	if($_GET['Condition']!="None")
		{
	$search[]=array('CONDITION',$_GET['Condition']);
}
}
if(isset($_GET['Style']))
{
	if($_GET['Style']!="None")
		{
	$search[]=array('BODY_STYLE',base64_decode($_GET['Style']));
		}
}
if(isset($_GET['Make']))
{
	if($_GET['Make']!="None")
		{
	$search[]=array('MAKE',base64_decode($_GET['Make']));
		}
}
if(isset($_GET['Mileage']))
{
	if($_GET['Mileage']!="None")
		{
	$search[]=array('MILEAGE',$_GET['Mileage']);
		}
}
if(isset($_GET['Trans']))
{
	if($_GET['Trans']!="None")
		{
	$search[]=array('TRANSMISSION',base64_decode($_GET['Trans']));
		}
}
if(isset($_GET['DTrain']))
{
	if($_GET['DTrain']!="None")
		{
	$search[]=array('DRIVE_TRAIN',base64_decode($_GET['DTrain']));
		}
}
if(isset($_GET['ExtColor']))
{
	if($_GET['ExtColor']!="None")
		{
	$search[]=array('EXTERIOR_COLOR',base64_decode($_GET['ExtColor']));
		}
}
if(isset($_GET['IntColor']))
{
	if($_GET['IntColor']!="None")
		{
	$search[]=array('INTERIOR_COLOR',base64_decode($_GET['IntColor']));
		}
}
if(isset($_GET['FuelType']))
{
	if($_GET['FuelType']!="None")
		{
	$search[]=array('FUEL_TYPE',base64_decode($_GET['FuelType']));
		}
}
if(isset($_GET['CityMiles']))
{
	if($_GET['CityMiles']!="None")
		{
	$search[]=array('CITY_MILES_PER_GALLON',$_GET['CityMiles']);
}
}
if(isset($_GET['HighWayMiles']))
{
	if($_GET['HighWayMiles']!="None")
		{
	$search[]=array('HIGHWAY_MILES_PER_GALLON',$_GET['HighWayMiles']);
		}
}
if(isset($_POST['Year'])){
if(isset($_POST['start_year']))
	{
		$startYear=1;
		
	}
	if(isset($_POST['end_year']))
	{
		$endYear=1;
	}
	if($startYear==1 && $endYear==1)
	{
		$search[]=array('START_YEAR',$_POST['start_year']);
		$search[]=array('END_YEAR',$_POST['end_year']);
	}
}

$test=array(array());

	if($search!=array()){
	$test=$CarsInfo->getFilteredCars($search[0][0],$search[0][1]);
	
	foreach($search as $key =>$searchCar){
	$test=$CarsInfo->FilterCares($test,$searchCar[0],$searchCar[1]);
	
	}
	
	
	if($test!=array())
	{
		$found=sizeof($test);
		
		if(isset($_GET['Sort'])){
		foreach($test as $key => $Cars)
		{
			if(isset($_GET['Sort']) &&$_GET['Sort']=="Year" )
			{
			$Sort[$key]=$Cars[5];
			}
			else if(isset($_GET['Sort']) &&$_GET['Sort']=="Make" )
			{
			$Sort[$key]=$Cars[2];
			}
			else if(isset($_GET['Sort']) &&$_GET['Sort']=="Model" )
			{
			$Sort[$key]=$Cars[22];
			}
			else if(isset($_GET['Sort']) &&$_GET['Sort']=="Mileage" )
			{
			$Sort[$key]=$Cars[6];
			}
			else if(isset($_GET['Sort']) &&$_GET['Sort']=="Price" )
			{
			$Sort[$key]=$Cars[15];
			}
		
		
		}
		array_multisort($Sort,SORT_DESC,$test);
		}

?><br>
		<div class="imageholder" style="height: auto;">
<span style=font-size:18px;font-weight:bold;color:#66cccc>Vehicle Found: <?php echo $found ?></span>
<?php
  foreach($test as $key => $Car)
  {?>
		<?php include_once 'popup.php';?>
<a href="viewVehicleDetails.php?stock_id=<?php echo $Car[0];?>" style="width: 500px">
	
    <table id="viewvehicles" border="0" style="text-align: left;margin-bottom: 13px;margin-top: -40px;border-style: groove;border-right: 2px solid #66cccc;border-left: 2px solid #66cccc;border-top: 2px solid #66cccc;border-bottom: 2px solid #66cccc ">
           <tr>
  <td>
    <h4><?php echo $Car[22]." ".$Car[2] ?></h4>
  </td>
  </tr>
       	<tr>
       		<td align="left">
       			<?php if($Car[1]==NULL){echo "No Title";}else{echo " ".$Car[1]; }?>
       		</td>
       	</tr>
		<tr>
       		<td>
       			Stock# <strong> <?php echo $Car[0]?></strong>
                        <strong style="float:right;margin-right:20px;color:#66cccc;font-size: 18px;font-weight:bold"><?php if($Car[14]==NULL){echo "Not Available";}else{echo $Car[14];}?></strong>

       		</td>
		</tr>       
       	<tr align="left">
       		<td align="left" width="800px" style="overflow: hidden;padding: 5px">
       			<img  width="150px" height="100px" style="float: left;border-style: groove;padding: 2px" src="<?php if($Car[17]==NULL){echo "images/car5.jpg";}else{echo $Car[17]; }?>">
                        <br><strong>&nbspEngine:</strong>&nbsp;<?php if($Car[20]==NULL){echo "Not Available";}else{echo $Car[20];}?><br>
                <strong>&nbspTransmission:</strong>&nbsp;<?php if($Car[7]==NULL){echo "Not Available";}else{echo $Car[7];}?><br>
				<strong>&nbspMilage:</strong>&nbsp;<?php if($Car[6]==NULL){echo "Not Available";}else{echo $Car[6];}?> Miles<br>
                <div id="Exterior" style="float:left"><strong>&nbspExterior:&nbsp;</strong></div><div id="exterior" style="float:left;width:12px;height:12px;background-color:<?php echo $Car[9];?>"></div>&nbsp;<?php echo $Car[9];?><br>
                <div id="Interior" style="float:left"><strong>&nbspInterior:&nbsp;&nbsp;</strong></div><div id="interior" style="width:12px;height:12px;background-color: <?php echo $Car[10] ?>"></div>&nbsp<?php echo $Car[10];?></br>
				<br><span style="color: white;">------------------------------------------------------------------------------------------------------------------------------------------------------------------------------</span>
				<strong style="font-size:18px;font-weight:bold;float:right;margin-right: 200px;margin-top: -30px">&nbspPrice $:&nbsp;<?php if($Car[15]==NULL){echo "Not Available";}else{echo $Car[15];}?></strong><br>				
                <br><strong style="float:left;margin-top: -10px">&nbsp<span style="border-style: ridge;border-color: white;">Photos:&nbsp;&nbsp;<?php echo $CarsInfo->getPhotos($Car[0]);?></span></strong><br>
                <a href="#login-box" class="info-popup" id="req:<?php echo $Car[17];?>:<?php echo $Car[1];?>:<?php echo $Car[7];?>:<?php echo $Car[6]?>:<?php echo $Car[0];?>" style="float:right;margin-top: -30px;color:#414141;border:3px solid #66cccc;border-radius:25px;-moz-border-radius:25px; background-color: #66cccc" ><strong>Request More Info</strong></a>
                
       			
       		</td>
       	</tr>
       	
       	</table></a><br>
      <?php } 
	}?>
</div>

<?php

	}

	else
	{?> 
	<div class="imageholder" style="height: auto">
	<?php
	
	if($Cars!=array())
	{
		$found=sizeof($Cars);
		echo "<span style=font-size:18px;font-weight:bold;color:#66cccc>Vehicles Found: ".$found."</span>";
		if(isset($_GET['Sort'])){
		foreach($Cars as $key => $Car)
		{
			if(isset($_GET['Sort']) &&$_GET['Sort']=="Year" )
			{
			$Sort[$key]=$Car[5];
			}
			else if(isset($_GET['Sort']) &&$_GET['Sort']=="Make" )
			{
			$Sort[$key]=$Car[2];
			}
			else if(isset($_GET['Sort']) &&$_GET['Sort']=="Model" )
			{
			$Sort[$key]=$Car[22];
			}
			else if(isset($_GET['Sort']) &&$_GET['Sort']=="Mileage" )
			{
			$Sort[$key]=$Car[6];
			}
			else if(isset($_GET['Sort']) &&$_GET['Sort']=="Price" )
			{
			$Sort[$key]=$Car[15];
			}
		
		
		}
		array_multisort($Sort,SORT_DESC,$Cars);
		}
	}
	foreach($Cars as $key1 => $Car){?>
    

<?php include_once 'popup.php';?>
<a href="viewVehicleDetails.php?stock_id=<?php echo $Car[0];?>">	
 <table id="vehicle" border="0" style="text-align: left;margin-bottom: 13px;margin-top:-40px;border-style: groove;border-right: 2px solid #66cccc;border-left: 2px solid #66cccc;border-top: 2px solid #66cdcc;border-bottom: 2px solid #66cccc ">

           <tr>
  <td>
    <h4><?php echo $Car[22]." ".$Car[2] ?></h4>
  </td>
  </tr>
       	<tr>
       		<td align="left" width="800px" style="overflow: hidden;">
       			<?php if($Car[1]==NULL){echo "No Title";}else{echo $Car[1]; }?>
       		</td>
       	</tr>
		<tr>
       		<td>
       			Stock# <strong> <?php echo $Car[0]?></strong>
                        <strong style="float:right;margin-right:20px;color:#66cccc;font-size: 18px;font-weight:bold"><?php if($Car[14]==NULL){echo "Not Available";}else{echo $Car[14];}?></strong>

       		</td>
		</tr>       
       	<tr align="left">
       		<td align="left" width="800px" style="overflow: hidden;padding: 5px">
       		   <img width="150px" height="100px" style="float: left;border-style: groove;"; alt="" src="<?php if($Car[17]==NULL){echo "images/car5.jpg";}else{echo $Car[17]; }?>">
               <br><strong>&nbspEngine:</strong>&nbsp;<?php if($Car[20]==NULL){echo "N/A";}else{echo " ".$Car[20]; }?><br>
               <strong>&nbspTransmission:</strong>&nbsp;<?php echo $Car[7];?><br>
			   <strong>&nbspMilage:</strong>&nbsp;<?php echo $Car[6];?> Miles<br>
               <div id="Exterior" style="float:left;"><strong>&nbspExterior:&nbsp;</strong></div><div id="exterior" style="float:left;width:12px;height:12px;background-color:<?php echo $Car[9];?>"></div>&nbsp;<?php echo $Car[9];?><br>
               <div id="Interior" style="float:left"><strong>&nbspInterior:&nbsp;&nbsp;</strong></div><div id="interior" style="width:12px;height:12px;background-color: <?php echo $Car[10] ?>"></div>&nbsp<?php echo $Car[10];?></br>
               <br><span style="color: white;">------------------------------------------------------------------------------------------------------------------------------------------------------------------------------</span>
               <strong style="font-size:18px;font-weight:bold;float:right;margin-right: 200px;margin-top: -40px">&nbspPrice $:&nbsp;<?php if($Car[15]==NULL){echo "Not Available";}else{echo $Car[15];}?></strong><br>				
               <br> <strong style="margin-top: -10px;float:left">&nbsp<span style="border-style: ridge;border-color: white;">Photos:&nbsp;&nbsp;<?php echo $CarsInfo->getPhotos($Car[0]);?></span></strong><br>               
       		   <a href="#login-box" class="info-popup" id="req:<?php echo $Car[17];?>:<?php echo $Car[1];?>:<?php echo $Car[7];?>:<?php echo $Car[6]?>:<?php echo $Car[0];?>" style="float:right;margin-top: -30px;color:#414141;border:3px solid #66cccc;border-radius:25px;-moz-border-radius:25px; background-color: #66cccc" ><strong>Request More Info</strong></a>
                </td>
       	</tr>
       	</table></a><br>

<?php	}
?>
</div>
<?php 	}

if($test==array())
{
	echo "<span style=font-size:18px;color:red;font-weight:bold>Results Not Found</span>";
}
?>